package cn.gogpay.dcb.api.web.base;

import cn.gogpay.dcb.common.dto.ResultCode;
import cn.gogpay.dcb.common.dto.ResultDTO;
import cn.gogpay.dcb.common.exception.ServiceException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ValidationException;

/**
 * 全局异常处理
 * @author huangting@gogpay.cn
 * @date 2019-10-14 9:33:38
 */
@ControllerAdvice
public class AdviceController {

    /**
     * 全局异常处理
     * 格式化异常返回
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultDTO handle(Exception ex) {
        if (ex instanceof ServiceException || ex instanceof ValidationException) {
            return new ResultDTO(ResultCode.serverError().code(),ex.getMessage());
        }
        ex.printStackTrace();
        return new ResultDTO(ResultCode.serverError().code(),"服务器异常");
    }
}